from django.db import models
from user.models import User
from product.models import Product
from utils import BaseModel
# Create your models here.


class Order(BaseModel):
    """订单模型"""
    status_choices = (
        (0, '未支付'),
        (1, '已支付'),
        (2, '已取消'),
        (3, '超时取消'),
    )
    pay_choices = (
        (1, '支付宝'),
        (2, '微信支付'),
    )
    subject = models.CharField(max_length=150, verbose_name="订单标题")
    # total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="订单总价", default=0)
    out_trade_no = models.CharField(max_length=64, verbose_name="订单号", unique=True)
    trade_no = models.CharField(max_length=64, null=True, verbose_name="流水号")
    order_status = models.SmallIntegerField(choices=status_choices, default=0, verbose_name="订单状态")
    pay_type = models.SmallIntegerField(choices=pay_choices, default=1, verbose_name="支付方式")
    pay_time = models.DateTimeField(verbose_name="支付时间",auto_now_add=True)
    user = models.ForeignKey(User, related_name='user_orders', on_delete=models.DO_NOTHING, db_constraint=False,
                             verbose_name="下单用户")
    total_amount = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="订单总价")
    go_time = models.DateTimeField(verbose_name='出发时间')
    product = models.ForeignKey(Product,related_name='product_orders',on_delete=models.DO_NOTHING,db_constraint=False,verbose_name='下单产品')
    peoples = models.IntegerField(verbose_name='出行人数')

    @property
    def status(self):
        return self.get_order_status_display()

    @property
    def product_cover(self):
        return self.product.cover



    class Meta:
        db_table = "travel_order"
        verbose_name = "订单记录"
        verbose_name_plural = "订单记录"

    def __str__(self):
        return "%s - ￥%s" % (self.subject, self.total_amount)